/**
* @file		Database.ixx
* @brief	游戏配置数据
*
*
* @version	1.0
* @author	lveyou
* @date		22-03-08
*
*/

module;
#include <pch.h>
export module Database;

import DND;

//----------------------------导出类-------------------------
//地形
export struct DbTerrain
{
	string _name;
	natural _id;

	bool operator==(string_view name) const
	{
		return name == _name;
	}
};
//朝代
export struct DbAge
{
	Point _range;//时间范围
	string _name;
};
//定居点
export struct DbLocation
{
	Point _xy;
	string _name;
};

//--------------------------非导出----------------------------
struct SLTerrain
{
	char _name[16];
	n16 _id;
};
struct SLAge
{
	z32 _age;//为起始公元时间
	char _name[32];
};
struct SLLocation
{
	char _name[32];
	char _area[32];
	floatx2 _xy;
};


export class Database
{
public:
	Database()
	{
		_load_db();

		_mapFacility2Name[0] = "location";
	}

	//! 失败返回-1
	n16 GetTerrainId(const string& name)
	{
		auto iter = _mapName2Id.find(name);
		if (iter == _mapName2Id.end())
			return -1;
		return iter->second;
	}

	string GetTerrainName(n16 id)
	{
		auto iter = _dbTerrain.find(id);
		if (iter == _dbTerrain.end())
			return {};
		return iter->second._name;
	}


	const map<natural, DbTerrain>& GetTerrainRef()
	{
		return _dbTerrain;
	}

	const vector<DbAge>& GetAgeRef()
	{
		return _dbAge;
	}

	//失败返回nullptr
	DbAge* GetAge(size_t index)
	{
		if (index >= _dbAge.size())
			return nullptr;
		return &_dbAge[index];
	}

	//以下标返回 时代名
	string GetAgeName(size_t index)
	{
		if (DbAge* age = GetAge(index))
			return age->_name;
		return "未知时代";
	}

	//以年返回 时代名
	string GetAgeName(zahlen year)
	{
		for (auto& iter : _dbAge)
		{
			if (year >= iter._range[0] && year < iter._range[1])
				return iter._name;
		}
		return "未知时代";
	}

	//以下标返回 时间范围
	Point GetAgeRange(size_t index)
	{
		if (DbAge* age = GetAge(index))
			return age->_range;
		return {};
	}

	const vector<DbLocation>& GetLocationRef()
	{
		return _dbLocation;
	}

private:
	map<natural, DbTerrain> _dbTerrain;
	map<std::string, natural> _mapName2Id;
	map<natural, string> _mapFacility2Name;
	vector<DbAge> _dbAge;
	vector<DbLocation> _dbLocation;

	void _load_db();
};

export Database* g_db;